package hc.bre.rest.auth

import static org.junit.jupiter.api.Assertions.*

import java.security.MessageDigest
import javax.crypto.KeyGenerator
import javax.crypto.SecretKey
import org.apache.shiro.SecurityUtils
import org.apache.shiro.authc.AuthenticationException
import org.apache.shiro.authc.UsernamePasswordToken
import org.apache.shiro.codec.Hex
import org.apache.shiro.crypto.hash.Md5Hash
import org.apache.shiro.crypto.hash.Sha256Hash
import org.apache.shiro.subject.Subject
import org.junit.jupiter.api.Test
import org.springframework.boot.test.context.SpringBootTest


//@SpringBootTest
@groovy.transform.CompileStatic
class BasicTest {

	@Test
	void string2bytes()
	{
		byte[] text = 'hello'.getBytes();
		assert 'hello' == new String(text);
	}

	@Test
	void javaBase64()
	{
		byte[] text = 'hello'.getBytes();
		byte[] text64code = Base64.encoder.encode(text);
		assert Base64.decoder.decode(text64code) == text;
	}

	@Test
	void javaBase64OfText()
	{
		String text = 'hello'
		String text64code = Base64.encoder.encodeToString(text.getBytes());
		println text64code;
		assert  text  == new String(Base64.decoder.decode(text64code));

	}

	@Test
	void shiroBase64()
	{
		String text = 'hello'
		String text64code =  org.apache.shiro.codec.Base64.encodeToString(text.getBytes());
		assert text64code == Base64.encoder.encodeToString(text.getBytes());
	}


	@Test
	void shiroHex()
	{
		String text = 'hello'
		assert Hex.encodeToString(text.getBytes()) =='68656c6c6f'
		assert new String(Hex.decode('68656c6c6f')) == text
	}

	@Test
	void Md5Hash()
	{
		String text = 'admin'
		Md5Hash md5t = new Md5Hash(text)
		//		println md5t.bytes
		assert  md5t.toString() ==md5t.toHex();

		MessageDigest md5 = MessageDigest.getInstance("md5");
		assert  md5t.bytes == md5.digest(text.getBytes())
	}

	@Test
	void Sha256()
	{
		String text = "123456";
		String salt = "wxKYXuTPST5SG0jMQzVPsg==";
		Sha256Hash sha256 = new Sha256Hash(text, salt,1024);
		assert  sha256.toBase64() == "J/ms7qTJtqmysekuY8/v1TAS+VKqXdH5sB7ulXZOWho=";
	}
	@Test
	void Sha2561()
	{
		String text = "999999";
		String salt = "my wechat id:sleepywang,my email:sleepwang@qq.com";
		Sha256Hash sha256 = new Sha256Hash(text, salt,2);
		println  '999999/my wechat id:sleepywang,my email:sleepwang@qq.com:' + sha256.toBase64();
	}
	
	@Test
	void AES()
	{
		KeyGenerator keygen = KeyGenerator.getInstance("AES");
		SecretKey deskey = keygen.generateKey();
		println "generated AES key:${Base64.encoder.encodeToString(deskey.getEncoded())}";
	}
	
	//	@Test
	void test() {
		//1、获取SecurityManager工厂，此处使用Ini配置文件初始化SecurityManager
		//		Factory<org.apache.shiro.mgt.SecurityManager> factory =
		//				null;
		//		//2、得到SecurityManager实例 并绑定给SecurityUtils
		//		org.apache.shiro.mgt.SecurityManager securityManager = factory.getInstance();

		Subject subject = SecurityUtils.getSubject();
		UsernamePasswordToken token = new UsernamePasswordToken("zhang", "123456");

		try {
			//4、登录，即身份验证
			subject.login(token);
		} catch (AuthenticationException e) {
			//5、身份验证失败
		}

		assert subject.isAuthenticated(); //断言用户已经登录

		//6、退出
		subject.logout();
	}

}
